﻿//----------------------------------------------------------------
// Copyright (c) 2011-2013 
// All rights reserved.
// Author: Ben DaoWei 
// Contact: bendaowei@gmail.com
// Web Link: http://xnaeditor.codeplex.com
// ----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using UnityEngine.Gameplay;

namespace UnityEngine.Terrain
{
    public enum EGridType
    {
        IsTree = 0,
        IsWalkBlocker = 1,
        IsFlyBlocker = 2,
        IsBuildBlocker = 3,
        IsDynamicWalkBlocker = 4,
        IsBlight = 5,
        IsNoWater = 6,
        IsCliff = 7,
    }

    //Flags table:
    //0x01: 0 (unused)
    //0x02: 1=no walk, 0=walk ok
    //0x04: 1=no fly, 0=fly ok
    //0x08: 1=no build, 0=build ok
    //0x10: 0 (unused)
    //0x20: 1=blight, 0=normal
    //0x40: 1=no water, 0=water
    //0x80: 1=unknown, 0=normal
    public class GridInformation
    {
        private WarCraftMapPath _MapPath;

        public bool IsTree
        {
            get
            {
                return (this.Data & (1 << (int)EGridType.IsTree)) != 0;
            }
        }

        public GridInformation(WarCraftMapPath owner)
        {
            _MapPath = owner;

            this.Data = 0;
            //this.IsValidForWalk = true;
        }

        public int Data
        {
            get;
            set;
        }

        private int TerrainBlocker = -1;
        public bool IsTerrainBlocker
        {
            get
            {
                if (TerrainBlocker >= 0)
                    return TerrainBlocker == 1;

                bool blocker = false;
                if ((this.Data & (1 << (int)EGridType.IsWalkBlocker)) != 0)
                    blocker = true;
                if ((this.Data & (1 << (int)EGridType.IsTree)) != 0)
                    blocker = true;
                if (blocker) TerrainBlocker = 1; else TerrainBlocker = 0;
                return blocker;
            }
        }

        private Dictionary<int, bool> _WalkBlocker = null;
        public bool IsWalkBlocker(int collisionSize)
        {
            return _WalkBlocker[collisionSize];
        }

        public void SetWalkBlocker(int collisionSize, bool blocker)
        {
            _WalkBlocker[collisionSize] = blocker;
        }

        public void UpdateWalkBlocker()
        {
            if (_WalkBlocker == null)
            {
                _WalkBlocker = new Dictionary<int, bool>();
                for (int i = 0; i <= OnSurfaceObject.MaxDynamicCollisizeSize; i++)
                    _WalkBlocker.Add(i, false);
            }

            for (int i = 0; i <= 0; i++)
            {
                bool blocker = false;
                if ((this.Data & (1 << (int)EGridType.IsWalkBlocker)) != 0)
                    blocker = true;
                if ((this.Data & (1 << (int)EGridType.IsDynamicWalkBlocker)) != 0)
                    blocker = true;
                if ((this.Data & (1 << (int)EGridType.IsTree)) != 0)
                    blocker = true;
                _WalkBlocker[i] = blocker;
            }
        }
    }
}
